import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import {bookInfo} from "../api/res"
export default new Vuex.Store({
  state: {
    bookList: [
      {
        "bookType": 1,
        "cover": "https://easyreadfs.nosdn.127.net/HYyjxr5LKszuzX5Undhuqw==/8796093024373293294",
        "title": "大夏王侯",
        "author": "一夕烟雨",
        "id": "audio_2efd65f7d2e24e8597e4d7f1535666ed_4",
      }
    ]
  },
  mutations: {
    AddBookList(state,obj){
      console.log(state,obj)
      state.bookList.unshift(obj)
      // console.log(state.bookList)
    },
    DeLBookList(state,index){
      state.bookList.splice(index,1)
      // console.log(state.bookList)
    }
  },
  actions: {
    addBookList(data,obj){
      // console.log(data,id)
      let index= data.state.bookList.findIndex(ele=>ele.id==obj.id)
      bookInfo({id:obj.id}).then(data=>{
        let entry =data.feed.entry;
        // console.log(entry.author.name,entry.title,obj.id,obj.cover)
        if(index==-1){
          this.commit("AddBookList",{
              bookType: 1,
              cover:obj.cover,
              title:entry.title,
              author:entry.author.name,
              id:obj.id
          })
        }
      })
    },
    deLBookList(data,id){
      let index= data.state.bookList.findIndex(ele=>ele.id==id);
      if(index!=-1){
        this.commit("DeLBookList",index)
      }
    }
  },
  modules: {
  }
})